package com.rw.coco.handler.message.reply.handler.command;

import com.rw.coco.api.reply.mapper.OptionReply;
import com.rw.coco.handler.message.event.CocoGroupMessageEvent;
import com.rw.coco.handler.message.reply.GroupReplyHandler;
import com.rw.coco.handler.message.reply.handler.command.assistant.CommandMatcher;
import com.rw.coco.handler.message.reply.handler.command.sender.ObSender;
import com.rw.coco.util.config.CocoCommandConfig;
import com.rw.coco.util.config.CocoConfig;
import com.rw.coco.util.config.CocoOption;
import com.rw.tool.util.string.StringUtil;

/**
 * 群OB指令处理
 * .ob
 */
public class GroupObHandler extends GroupReplyHandler {
    private final CommandMatcher matcher;

    public final boolean funcSwitch;

    private static final String EXIT = "exit";

    private static final String LIST = "list";

    private static final String CLR = "clr";

    private static final String ON = "on";

    private static final String OFF = "off";

    public GroupObHandler(CocoConfig cocoConfig) {
        CocoCommandConfig cocoCommandConfig = cocoConfig.getCocoCommandConfig();
        CocoOption cocoOption = cocoConfig.getCocoOption();

        String command = cocoCommandConfig.commandProp.get(CocoCommandConfig.GROUP_OB);
        this.matcher = CommandMatcher.of(command);
        this.funcSwitch = cocoOption.optionProp.getBoolean(CocoOption.COCO_OB);
    }

    @Override
    protected boolean groupTrigger(CocoGroupMessageEvent groupEvent) {
        String message = groupEvent.getMessage();
        return funcSwitch && matcher.isMatch(message);
    }

    @Override
    protected void groupHandler(CocoGroupMessageEvent groupEvent) {
        // 去掉ob前缀
        String message = matcher.subTrim(groupEvent.getMessage());
        OptionReply optionReply = groupEvent.getReplyApi().getOptionReply();

        // 构建回复对象
        ObSender obSender = new ObSender(groupEvent);

        if (StringUtil.isBlank(message)) {
            // 加入群旁观
            obSender.obIn();
        } else if (EXIT.equalsIgnoreCase(message)) {
            // 退出群旁观
            obSender.obExit();
        } else if (LIST.equalsIgnoreCase(message)) {
            // 查看群旁观列表
            obSender.obList();
        } else if (CLR.equalsIgnoreCase(message)) {
            // 清空群旁观列表
            obSender.obClear();
        } else if (ON.equalsIgnoreCase(message)) {
            // 群旁观开关开启
            obSender.obOn();
        } else if (OFF.equalsIgnoreCase(message)) {
            // 群旁观开关关闭
            obSender.obOff();
        } else {
            // 未知指令
            groupEvent.sendBack(optionReply.obUnknown(groupEvent, message));
        }
    }

    @Override
    public int priority() {
        return matcher.priority();
    }
}